package physics;

import java.awt.geom.Point2D;


/**
 * A utility class representing a Euclidean vector.
 * 
 * @author geo4
 * 
 */
public class EuclideanVector {

    private double myX;
    private double myY;

    /**
     * Creates a new vector with the dimensions (0, 0).
     */
    public EuclideanVector () {
        this(0, 0);
    }

    /**
     * Creates a new vector with the dimension specified.
     * 
     * @param x x component of the vector
     * @param y y component of the vector
     */
    public EuclideanVector (double x, double y) {
        myX = x;
        myY = y;
    }

    /**
     * Returns the direction of the vector as a value between 0 and 2*PI.
     * 
     * @return the angle of the vector in radians
     */
    public double getDirection () {
        return Math.atan2(myY, myX);
    }

    /**
     * Finds the length of the vector.
     * 
     * @return magnitude of the vector
     */
    public double getMagnitude () {
        return Math.sqrt(Math.pow(myX, 2) + Math.pow(myY, 2));
    }

    /**
     * Finds the length of x component of the vector.
     * 
     * @return x component of the vector
     */
    public double getX () {
        return myX;
    }

    /**
     * Finds the length of y component of the vector.
     * 
     * @return y component of the vector
     */
    public double getY () {
        return myY;
    }

    /**
     * Changes the length of the vector by the scalar specified.
     * 
     * @param scalar value by which to scale the vector
     */
    public void scale (double scalar) {
        myX = myX * scalar;
        myY = myY * scalar;
    }

    /**
     * Adds the Euclidean vector to a two-dimensional point.
     * 
     * @param point Point2D to add the vector values to
     * @return point after the x and y components of the vector have been added
     *         to it
     */
    public Point2D addToPoint (Point2D point) {
        point.setLocation(point.getX() + myX, point.getY() + myY);
        return point;
    }

    /**
     * Adds the components of the other EuclideanVector to this vector.
     * 
     * @param other EuclideanVector with which to add the vector
     * @return this vector after the addition of the other vector
     */
    public EuclideanVector add (EuclideanVector other) {
        myX = myX + other.getX();
        myY = myY + other.getY();
        return this;
    }
}
